package com.example.lj.uav.view;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.lj.uav.MyApplication;
import com.example.lj.uav.R;
import com.example.lj.uav.view.base.BaseActivity;
import com.example.lj.uav.widget.LoadingDialog;

import java.util.List;

import dji.common.error.DJIError;
import dji.common.flightcontroller.flyzone.FlyZoneInformation;
import dji.common.util.CommonCallbacks;
import dji.sdk.flightcontroller.FlyZoneManager;

public class DissolutionHistoryActivity extends BaseActivity implements View.OnClickListener {

    private static Handler mHandler;
    private RecyclerView rv_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dissolutionhistory);
        mHandler = new Handler(Looper.getMainLooper());
        findViewById(R.id.dissolution_fl_back).setOnClickListener(this);
        findViewById(R.id.dissolution_fl_info).setOnClickListener(this);
        findViewById(R.id.dissolution_fl_refresh).setOnClickListener(this);

        rv_content = findViewById(R.id.dissolution_rv_content);

        getUnlockInfo();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dissolution_fl_back:
                finish();
                break;
            case R.id.dissolution_fl_info:
                //TODO
                break;
            case R.id.dissolution_fl_refresh:
                getUnlockInfo();
                break;
        }
    }

    private void getUnlockInfo() {
        LoadingDialog ld = LoadingDialog.getInstance(this);
        ld.show();
        FlyZoneManager flyZoneManager = MyApplication.getFlyZoneInstance();
        flyZoneManager.getUnlockedFlyZonesForAircraft(new CommonCallbacks.CompletionCallbackWith<List<FlyZoneInformation>>() {
            @Override
            public void onSuccess(List<FlyZoneInformation> flyZoneInformations) {
                mHandler.post(new TextRunnable(flyZoneInformations));
            }

            @Override
            public void onFailure(DJIError djiError) {
                mHandler.post(new TextRunnable("0"));
            }
        });
        ld.dismiss();
    }

    private class TextRunnable implements Runnable {

        private String num;

        private TextRunnable(String s) {
            num = s;
        }

        private TextRunnable(List<FlyZoneInformation> list) {
            num = list.size() + "";
        }

        @Override
        public void run() {
            ((TextView) findViewById(R.id.dissolution_tv_credential)).setText(
                    getResources().getString(R.string.my_unlock_credential, num));
        }
    }
}
